﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CuitHtmlList.cs" company="">
//   
// </copyright>
// <summary>
//   The cuit html list.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace UITest.CUIT.CuitControls.HtmlControls
{
    using System;
    using System.Linq;

    using Microsoft.VisualStudio.TestTools.UITesting;
    using Microsoft.VisualStudio.TestTools.UITesting.HtmlControls;

    /// <summary>
    /// The cuit html list.
    /// </summary>
    public class CuitHtmlList : CuitHtmlControl<HtmlList>
    {
        #region Constructors and Destructors

        /// <summary>
        ///     Initializes a new instance of the <see cref="CuitHtmlList" /> class.
        /// </summary>
        public CuitHtmlList()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitHtmlList"/> class.
        /// </summary>
        /// <param name="testControl">
        /// The test control.
        /// </param>
        public CuitHtmlList(UITestControl testControl)
            : base(testControl)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitHtmlList"/> class.
        /// </summary>
        /// <param name="testControl">
        /// The test control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        public CuitHtmlList(UITestControl testControl, string searchProperties)
            : base(testControl, searchProperties)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitHtmlList"/> class.
        /// </summary>
        /// <param name="testControl">
        /// The test control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        /// <param name="filterProperties">
        /// The filter properties.
        /// </param>
        public CuitHtmlList(UITestControl testControl, string searchProperties, string filterProperties)
            : base(testControl, searchProperties, filterProperties)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitHtmlList"/> class.
        /// </summary>
        /// <param name="wrappedControl">
        /// The wrapped control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        public CuitHtmlList(ICuitControlBase wrappedControl, string searchProperties)
            : base(wrappedControl, searchProperties)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitHtmlList"/> class.
        /// </summary>
        /// <param name="wrappedControl">
        /// The wrapped control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        /// <param name="filterProperties">
        /// The filter properties.
        /// </param>
        public CuitHtmlList(ICuitControlBase wrappedControl, string searchProperties, string filterProperties)
            : base(wrappedControl, searchProperties, filterProperties)
        {
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets the items.
        /// </summary>
        public string[] Items
        {
            get
            {
                // trying to call InnerText of children will cause errors if child items are disabled
                return this._control.InnerText.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            }
        }

        /// <summary>
        ///     Gets or sets the selected item as string.
        /// </summary>
        public string SelectedItemAsString
        {
            get
            {
                this._control.WaitForControlReady();
                return this._control.SelectedItemsAsString;
            }

            set
            {
                this._control.WaitForControlReady();
                this._control.SelectedItemsAsString = value;
                this.WriteTestOutput("Selected Item " + value + " on ");
            }
        }

        /// <summary>
        ///     Gets or sets the selected items.
        /// </summary>
        public string[] SelectedItems
        {
            get
            {
                this._control.WaitForControlReady();
                return this._control.SelectedItems;
            }

            set
            {
                this._control.WaitForControlReady();
                this._control.SelectedItems = value;
                this.WriteTestOutput(string.Join(",", value));
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The item exists.
        /// </summary>
        /// <param name="text">
        /// The text.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool ItemExists(string text)
        {
            return this.Items.Contains(text);
        }

        #endregion
    }
}